package com.potenza.training.resume.controller;


import com.potenza.training.dictionary.entity.HopeAddress;
import com.potenza.training.dictionary.entity.HopePosition;
import com.potenza.training.resume.config.ExportWord;
import com.potenza.training.resume.entity.ItemExperienceEntity;
import com.potenza.training.resume.entity.ResumeBasicEntity;
import com.potenza.training.resume.entity.WorkExperienceEntity;
import com.potenza.training.resume.feign.DictionartFeig;
import com.potenza.training.resume.service.ResumeBasicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("resume/word")
public class WordController {
    @Autowired
    ResumeBasicService resumeBasicService;
    @Autowired
    DictionartFeig dictionartFeig;
    //个人基本信息
//    ResumeBasicEntity resumeBasicEntity  =resumeBasicService.findResumeBasic(resumeId);
//    //优势
//    List<AdvantageEntity> advantageEntityList= resumeBasicService.findAdvange(resumeId);
//    //工作经历
//    List<WorkExperienceEntity> workExperienceEntityList=  resumeBasicService.findWorkExpersoin(resumeId);
//    //求职期望
//    List<ResumeHope> resumeHopeList=resumeBasicService.findResumeHope(resumeId);
//    //教育经历
//    List<EducationExperienceEntity> educationExperienceEntityList=resumeBasicService.findEducation(resumeId);
//    //项目经历
//    List<ItemExperienceEntity> itemExperienceEntityList=resumeBasicService.findItem(resumeId);
//    //社交作品
//    List<SocialEntity> socialEntityList=resumeBasicService.findSocial(resumeId);

    @RequestMapping(value = "/exportWord/{resumeId}", method= RequestMethod.GET)
    public void exportWord(HttpServletRequest request, HttpServletResponse response, @PathVariable Integer resumeId) throws Exception {
        String fileName = "简历生成.doc"; //文件名称
        Map<String,Object> dataMap = new HashMap<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //基本信息
        ResumeBasicEntity resumeBasicEntity  =resumeBasicService.findResumeBasic(resumeId);
        String address = resumeBasicEntity.getAddress();
        String substring = address.substring(0, address.length() - 1);
        String[] split = substring.split(",");
        String s = split[split.length - 1];
        HopeAddress byIdtow = dictionartFeig.findByIdtow(s);
        resumeBasicEntity.setAddress(byIdtow.getAddress());
        dataMap.put("resumeBasicEntity",resumeBasicEntity);

        //工作经历
        List<WorkExperienceEntity> workList=  resumeBasicService.findWorkExpersoin(resumeId);
        ArrayList<WorkExperienceEntity> workExperienceEntityList = new ArrayList<>();
        for (WorkExperienceEntity workExperienceEntity : workList) {
            String type = workExperienceEntity.getType();
            String substring1 =  type.substring(0, type.length() - 1);
            String[] split1 = substring1.split(",");
            String s1 = split1[split1.length - 1];
            HopePosition byId = dictionartFeig.findById(s1);
            String format = simpleDateFormat.format(workExperienceEntity.getOutDate());
            workExperienceEntity.setEndTimeString(format);
            workExperienceEntity.setType(byId.getName());
            workExperienceEntityList.add(workExperienceEntity);
        }
        dataMap.put("workExperienceEntityList",workExperienceEntityList);
        //项目经历
        List<ItemExperienceEntity> itemList=resumeBasicService.findItem(resumeId);
        ArrayList<ItemExperienceEntity> itemExperienceEntityList = new ArrayList<>();
        for (ItemExperienceEntity itemExperienceEntity : itemList) {
            String format = simpleDateFormat.format(itemExperienceEntity.getItemOutDate());
            itemExperienceEntity.setEndTimeString(format);
            itemExperienceEntityList.add(itemExperienceEntity);
        }
        dataMap.put("itemExperienceEntityList",itemExperienceEntityList);
        new ExportWord("UTF-8").exportDoc(response, fileName, "cs.ftl", dataMap);
    }



}


